package com.kyzh.core.pager.weal.community.adapter

import android.content.res.ColorStateList
import android.graphics.Color
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.atools.util.setWidth
import com.gushenge.core.beans.FragCommunityBean
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemCircleImageBinding
import com.kyzh.core.databinding.ItemFragCommunityBinding
import com.kyzh.core.utils.ViewUtils.dp
import com.kyzh.core.utils.reviewImages
import com.kyzh.core.utils.setConfig
import com.kyzh.core.utils.setVisibility
import com.littlejerk.rvdivider.builder.XGridBuilder

class CommunityNewsAdapter :BaseQuickAdapter<FragCommunityBean.Data,BaseDataBindingHolder<ItemFragCommunityBinding>>( R.layout.item_frag_community
) ,LoadMoreModule{
    init {
        addChildClickViewIds(R.id.delect)
    }
    override fun convert(
        holder: BaseDataBindingHolder<ItemFragCommunityBinding>,
        item: FragCommunityBean.Data
    ) {
        holder.dataBinding?.apply {
            d = item
            tvTag2.setVisibility(!item.video.isNullOrBlank())
            tvContent.setVisibility(!item.content.isNullOrBlank())
            tvTag.setVisibility(!item.biaoqian.isNullOrBlank())


            when {
                item.isBest=="1" -> {
                    tvTag1.setVisibility(true)
                    tvTag1.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#FA8C08"))
                    tvTag1.text = "置顶"
                }
                item.isJiajing == "1" -> {
                    tvTag1.setVisibility(true)
                    tvTag1.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#FF726A"))
                    tvTag1.text = "精华"
                }
                else -> {
                    tvTag1.setVisibility(false)
                }
            }
            if(!item.video.isNullOrBlank()){
                frame.setVisibility(true)
                video.setVisibility(true)
                rev.setVisibility(false)
                if(item.image!=null  && item.image.isNotEmpty()){
                    video.setConfig(item.video,item.image[0],holder.layoutPosition)
                }else{
                    video.setConfig(item.video,"",holder.layoutPosition)
                }
            }else if(item.image!=null  && item.image.isNotEmpty() &&item.image.size>0 ){
                video.setVisibility(false)
                frame.setVisibility(true)
                rev.setVisibility(true)
                if( item.image.size==1){
                    rev.setWidth(208.dp)
                }else{
                    rev.setWidth(339.dp)
                }
                val gridLayoutManager = GridLayoutManager(context, 6)
                val value = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {
                        return when (item.image.size) {
                            1 -> 6
                            2 -> 3
                            else -> 2
                        }
                    }
                }

                gridLayoutManager.spanSizeLookup =value
                if (rev.itemDecorationCount > 0) {
//                    val itemDecorationAt = rev.getItemDecorationAt(0)
//                    if (itemDecorationAt == null) {
//                        rev.addItemDecoration(ver!!.build())
//                    }
                } else {
                    val ver =
                        XGridBuilder(context).setSpacing(3f)//这几个Spacing的优先级可看XGridBuilder说明
                            //这几个color和drawable的优先级可看XGridBuilder说明
                            .setColor(Color.BLUE)
                            //可设置颜色和drawable,drawable>color
                            .setHLineColor(Color.WHITE)
                            .setVLineColor(Color.WHITE)
                            //是否包括边界
                            .setIncludeEdge(false)
                            //竖直和水平分割线交叉处绘制的是竖直分割线颜色（交叉处属于竖直分割线）
                            .setVerticalIncludeEdge(true)
                    rev.addItemDecoration(ver!!.build())
                }
                rev.layoutManager = gridLayoutManager

                val gameDetailBannerAdapter = object :BaseQuickAdapter<String,BaseDataBindingHolder<ItemCircleImageBinding>>(R.layout.item_circle_image,item.image){
                    override fun convert(
                        holder: BaseDataBindingHolder<ItemCircleImageBinding>,
                        item: String
                    ) {
                        holder.dataBinding?.d = item
                    }
                }
                rev.adapter = gameDetailBannerAdapter
                gameDetailBannerAdapter.setOnItemClickListener { adapter, view, position ->
                   context. reviewImages(item.image, position)
                }
            }else{
                video.setVisibility(false)
                rev.setVisibility(false)
                frame.setVisibility(false)
            }

        }
    }
}